<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Grade;
use App\Model\SportsMeeting;
use App\Model\Tournament;
use App\Model\Athlete;
use App\Model\Team;

class GradeController extends Controller
{
    //
    function index(){
        // 查出所有成绩
        $grade = Grade::all();
        // 初始化变量：运动会名，项目名，团队名，运动员名，积分类型
        $sportsmeeting_name = "";
        $tournament_name = "";
        $team_name = "";
        $athlete_name = "";
        $category_name = "";
        // 定义空数组，用于存放组合数据
        $data = array();
        // 循环成绩
        foreach($grade as $vo){
            // 根据运动会id查询运动会
            $sportsmeeting = SportsMeeting::find($vo->sports_meeting_id);
            if(!empty($sportsmeeting)){
                // 赋值
                $sportsmeeting_name = $sportsmeeting->name;
            }
            // 根据项目id查询项目
            $tournament = Tournament::find($vo->tournament_id);
            if(!empty($tournament)){
                $tournament_name = $tournament->name;
                // 项目计分类型设置值
                $category = $tournament->category;
                switch($category){
                    case 1001:
                        $category_name = "时间(s)";
                        break;
                    case 2001:
                        $category_name = "距离(m)";
                        break;
                    case 3001:
                        $category_name = "高度(cm)";
                        break;
                    case 4001:
                        $category_name = "积分(分)";
                        break;
                    default:
                        $category_name = "时间(s)";
                        break;

                }
                
            }
            // 根据团队id查询团队
            $team = Team::find($vo->team_id);
            if(!empty($team)){
                // 赋值
                $team_name = $team->name;
            }
            // 根据运动员id查询运动员
            $athlete = Athlete::find($vo->athlete_id);
            if(!empty($athlete)){
                // 赋值
                $athlete_name = $athlete->name;
            }
            // 将所有的值存储在新的关联数组中
            $new_arr = array(
                "sportsmeeting_name" => $sportsmeeting_name,
                "tournament_name" => $tournament_name,
                "team_name" => $team_name,
                "athlete_name" => $athlete_name,
                "round" => $vo->round,
                "category_name" => $category_name,
                "grade" => $vo->grade
            );
            // 将新数组添加到data数组中
            array_push($data,$new_arr);

        }

        // 返回视图，带上data数据
        return view('admin.grade.index',['data' => $data]);
    }

    function search(Request $request){
        // 获取表单数据
        $data = $request->input('data');
        
        $sportsmeeting = $data['sportsmeeting'];
        $tournament = $data['tournament'];
        $team = $data['team'];
        $athlete = $data['athlete'];

        // 只填了运动会
        if(!empty($sportsmeeting)&& empty($tournament)&& empty($team)&& empty($athlete)){
            // 第一个对象为运动会
            $obj1 = SportsMeeting::where('name',$sportsmeeting)->first();
            // 判断一个对象不能为空
            if(!empty($obj1)){
                // 运动会id
                $obj1_id = $obj1->id;
                // 查询条件
                $search = [
                ["sports_meeting_id","=",$obj1_id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
                
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }
        // 运动会、比赛项目
        if(!empty($sportsmeeting)&& !empty($tournament)&& empty($team)&& empty($athlete)){
            // 第一个对象为运动会
            $obj1 = SportsMeeting::where('name',$sportsmeeting)->first();
            // 第二个对象为比赛项目
            $obj2 = Tournament::where('name',$tournament)->first();
            // 判断两个对象不能为空
            if(!empty($obj1)&&!empty($obj2)){
                $search = [
                ["sports_meeting_id","=",$obj1->id],
                ["tournament_id","=",$obj2->id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }
        // 运动会、比赛项目、团队
        if(!empty($sportsmeeting)&& !empty($tournament)&& !empty($team)&& empty($athlete)){
            // 第一个对象为运动会
            $obj1 = SportsMeeting::where('name',$sportsmeeting)->first();
            // 第二个对象为比赛项目
            $obj2 = Tournament::where('name',$tournament)->first();
            // 第三个对象为团队
            $obj3 = Team::where('name',$team)->first();
            // 判断三个对象不能为空
            if(!empty($obj1)&&!empty($obj2)&&!empty($obj3)){
                $search = [
                ["sports_meeting_id","=",$obj1->id],
                ["tournament_id","=",$obj2->id],
                ["team_id","=",$obj3->id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }
        // 运动会、比赛项目、团队、远动员
        if(!empty($sportsmeeting)&& !empty($tournament)&& !empty($team)&& !empty($athlete)){
            // 第一个对象为运动会
            $obj1 = SportsMeeting::where('name',$sportsmeeting)->first();
            // 第二个对象为比赛项目
            $obj2 = Tournament::where('name',$tournament)->first();
            // 第三个对象为团队
            $obj3 = Team::where('name',$team)->first();
            // 第四个对象为运动员
            $obj4 = Athlete::where('name',$athlete)->first();
            // 判断四个对象不能为空
            if(!empty($obj1)&&!empty($obj2)&&!empty($obj3)&&!empty($obj4)){
                $search = [
                ["sports_meeting_id","=",$obj1->id],
                ["tournament_id","=",$obj2->id],
                ["team_id","=",$obj3->id],
                ["athlete_id","=",$obj4->id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }


        // 只填了比赛项目
        if(empty($sportsmeeting)&& !empty($tournament)&& empty($team)&& empty($athlete)){
            // 第二个对象为比赛项目
            $obj2 = Tournament::where('name',$tournament)->first();
            // 判断对象不能为空
            if(!empty($obj2)){
                // 比赛项目id
                $obj2_id = $obj2->id;
                // 查询条件
                $search = [
                ["tournament_id","=",$obj2->id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
                
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }
        // 只填了团队
        if(empty($sportsmeeting)&& empty($tournament)&& !empty($team)&& empty($athlete)){
            // 第三个对象为团队
            $obj3 = Team::where('name',$team)->first();
            // 判断对象不能为空
            if(!empty($obj3)){
                // 团队id
                $obj3_id = $obj3->id;
                // 查询条件
                $search = [
                ["team_id","=",$obj3->id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
                
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }
        // 只填了运动员
        if(empty($sportsmeeting)&& empty($tournament)&& empty($team)&& !empty($athlete)){
            // 第四个对象为运动员
            $obj4 = Athlete::where('name',$athlete)->first();
            if(!empty($obj4)){
                // 团队id
                $obj4_id = $obj4->id;
                // 查询条件
                $search = [
                ["athlete_id","=",$obj4->id]
                ];
                // 查询成绩
                $grade = Grade::where($search)->get();
                // 根据成绩调用toSearch函数获取重新组合的数据
                $data = $this->toSearch($grade);
                // 返回视图，带上data数据
                return view('admin.grade.index',['data' => $data]);
                
            }
            // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
            $data = $this->setNull();
            // 返回视图，带上data数据
            return view('admin.grade.index',['data' => $data]);
        }
        // 如果查不到对应的数据，则调用setNul函数，设置所有属性为“无”
        $data = $this->setNull();
        // 返回视图，带上data数据
        return view('admin.grade.index',['data' => $data]);
    }


    function toSearch($grade){
        // 初始化数据
        $sportsmeeting_name = "";
        $tournament_name = "";
        $team_name = "";
        $athlete_name = "";
        $category_name = "";
        // 定义数组
        $data = array();
        // 循环添加数据
        foreach($grade as $vo){
            $sportsmeeting = SportsMeeting::find($vo->sports_meeting_id);
            if(!empty($sportsmeeting)){
                $sportsmeeting_name = $sportsmeeting->name;
            }
            $tournament = Tournament::find($vo->tournament_id);
            if(!empty($tournament)){
                $tournament_name = $tournament->name;
                $category = $tournament->category;
                switch($category){
                    case 1001:
                        $category_name = "时间(s)";
                        break;
                    case 2001:
                        $category_name = "距离(m)";
                        break;
                    case 3001:
                        $category_name = "高度(cm)";
                        break;
                    case 4001:
                        $category_name = "积分(分)";
                        break;
                    default:
                        $category_name = "时间(s)";
                        break;

                }
                
            }
            $team = Team::find($vo->team_id);
            if(!empty($team)){
                $team_name = $team->name;
            }
            $athlete = Athlete::find($vo->athlete_id);
            if(!empty($athlete)){
                $athlete_name = $athlete->name;
            }
            $new_arr = array(
                "sportsmeeting_name" => $sportsmeeting_name,
                "tournament_name" => $tournament_name,
                "team_name" => $team_name,
                "athlete_name" => $athlete_name,
                "round" => $vo->round,
                "category_name" => $category_name,
                "grade" => $vo->grade
            );
            array_push($data,$new_arr);

        }

        if(empty($grade)||empty($data)){
            $data = $this->setNull();
        }
        
        return $data;
    }

    function setNull(){
        $data = array([
                    "sportsmeeting_name" => "无",
                    "tournament_name" => "无",
                    "team_name" => "无",
                    "athlete_name" => "无",
                    "round" => "无",
                    "category_name" => "无",
                    "grade" => "无"]
                );
        return $data;
    }


    
}
